/*
PWM波输出配置步骤
1.配置定时器基础工作参数   HAL_TIM_PWM_Init()
2.定时器PWM输出MSP初始化   HAL_TIM_PWM_MspInit()
3.配置PWM模式/比较值       HAL_TIM_PWM_ConfigChannel()
4.使能输出并启动计数器      HAL_TIM_PWM_Start()
5.修改比较值控制占空比      HAL_TIM_SET_COMPARE()
6.使能通道预装载
*/
#include "./BSP/TIMER/tim.h"

TIM_HandleTypeDef htim10;
TIM_HandleTypeDef htim11;

// MotorA_PWM
void HAL_TIM10_PWM_init(uint16_t arr, uint16_t psc)
{
    TIM_OC_InitTypeDef tim10_oc_pwm_chy;

    htim10.Instance = TIM10;
    htim10.Init.Prescaler = psc;
    htim10.Init.Period = arr;
    htim10.Init.CounterMode = TIM_COUNTERMODE_UP;
    htim10.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    htim10.Init.AutoReloadPreload = TIM_AUTOMATICOUTPUT_DISABLE;
    HAL_TIM_PWM_Init(&htim10);

    tim10_oc_pwm_chy.OCMode = TIM_OCMODE_PWM1;
    tim10_oc_pwm_chy.Pulse = arr / 2;
    tim10_oc_pwm_chy.OCPolarity = TIM_OCPOLARITY_HIGH;
    tim10_oc_pwm_chy.OCFastMode = TIM_OCFAST_DISABLE;

    HAL_TIM_PWM_ConfigChannel(&htim10, &tim10_oc_pwm_chy, TIM_CHANNEL_1);

    HAL_TIM_PWM_Start(&htim10, TIM_CHANNEL_1);
}

void HAL_TIM11_PWM_Init(uint16_t arr, uint16_t psc)
{
    TIM_OC_InitTypeDef tim11_oc_pwm_chy;

    htim11.Instance = TIM11;
    htim11.Init.Prescaler = psc;
    htim11.Init.Period = arr;
    htim11.Init.CounterMode = TIM_COUNTERMODE_UP;
    htim11.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    htim11.Init.AutoReloadPreload = TIM_AUTOMATICOUTPUT_DISABLE;

    HAL_TIM_PWM_Init(&htim11);

    tim11_oc_pwm_chy.OCMode = TIM_OCMODE_PWM1;
    tim11_oc_pwm_chy.Pulse = arr / 2;
    tim11_oc_pwm_chy.OCPolarity = TIM_OCPOLARITY_HIGH;
    tim11_oc_pwm_chy.OCFastMode = TIM_OCFAST_DISABLE;

    HAL_TIM_PWM_ConfigChannel(&htim11, &tim11_oc_pwm_chy, TIM_CHANNEL_1);

    HAL_TIM_PWM_Start(&htim11, TIM_CHANNEL_1);
}
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *timhandle)
{
    if (timhandle->Instance == TIM10)
    {
        GPIO_InitTypeDef gpio_init_struct;

        __HAL_RCC_GPIOB_CLK_ENABLE();
        __HAL_RCC_TIM10_CLK_ENABLE();

        gpio_init_struct.Pin = GPIO_PIN_8;
        gpio_init_struct.Mode = GPIO_MODE_AF_PP;
        gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH;
        gpio_init_struct.Pull = GPIO_NOPULL;
        gpio_init_struct.Alternate = GPIO_AF3_TIM10;

        HAL_GPIO_Init(GPIOB, &gpio_init_struct);
    }
    else if (timhandle->Instance == TIM11)
    {
        GPIO_InitTypeDef gpio_init_struct;

        __HAL_RCC_GPIOB_CLK_ENABLE();
        __HAL_RCC_TIM11_CLK_ENABLE();

        gpio_init_struct.Pin = GPIO_PIN_9;
        gpio_init_struct.Mode = GPIO_MODE_AF_PP;
        gpio_init_struct.Pull = GPIO_NOPULL;
        gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH;
        gpio_init_struct.Alternate = GPIO_AF3_TIM11;

        HAL_GPIO_Init(GPIOB, &gpio_init_struct);

        /* code */
    }
}
